﻿using CommonDevelop;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Text;

namespace WebAdmin
{
    /// <summary>
    /// 路径供应器
    /// </summary>
    public class PathProvider : IPathProvider
    {
        private IWebHostEnvironment _hostingEnvironment;
        /// <summary>
        /// 构造
        /// </summary>
        public PathProvider(IWebHostEnvironment environment)
        {
            _hostingEnvironment = environment;
        }

        /// <summary>
        /// API中使用
        /// </summary>
        public string WebRootPath
        {
            get { return _hostingEnvironment.WebRootPath ?? _hostingEnvironment.ContentRootPath; }
        }

        /// <summary>
        /// 取服务端路径
        /// </summary>
        public string MapPath(string path)
        {
            return WebRootPath + path;
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="Path">创建到路径</param>
        /// <param name="IsDateFolder">是否按日期创建文件夹</param>
        /// <param name="Status">是否创建成功(如果为false，将是文件已存在)</param>
        /// <returns>成功后返回文件夹路径，创建失败为null</returns>
        public string CreateFolder(string Path, bool IsDateFolder, out bool Status)
        {
            Status = false;
            string FolderName = null;
            DateTime dateTime = DateTime.Now;
            string absPath = this.MapPath("/") + Path;
            string dir = dateTime.ToString(@"\\yyyyMMdd");
            if (IsDateFolder)
            {
                if (!Directory.Exists(absPath + dir))
                {
                    Directory.CreateDirectory(absPath + dir);
                    Status = true;
                }
                FolderName = absPath + dir;
            }
            else
            {
                if (!Directory.Exists(absPath))
                {
                    Directory.CreateDirectory(absPath);
                    Status = true;
                }
                FolderName = absPath;
            }
            return FolderName;
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        public string CreateFolder(string Path, bool IsDateFolder)
        {
            string FolderName = null;
            DateTime dateTime = DateTime.Now;
            string absPath = this.MapPath(Path);
            string dir = dateTime.ToString(@"\\yyyyMMdd");
            if (IsDateFolder)
            {
                if (!Directory.Exists(absPath + dir))
                    Directory.CreateDirectory(absPath + dir);

                FolderName = absPath + dir;
            }
            else
            {
                if (!Directory.Exists(absPath))
                    Directory.CreateDirectory(absPath);

                FolderName = absPath;
            }
            return FolderName;
        }

        public string GetAbsoluteUri(HttpRequest request)
        {
            if (request == null)
                return string.Empty;

            return new StringBuilder()
                .Append(request.Scheme)
                .Append("://")
                .Append(request.Host)
                .Append(request.PathBase)
                .Append(request.Path)
                .Append(request.QueryString)
                .ToString();
        }

        public string GetHostUri(HttpRequest request)
        {
            if (request == null)
                return string.Empty;

            return new StringBuilder()
                .Append(request.Scheme)
                .Append("://")
                .Append(request.Host)
                .Append(request.PathBase)
                .ToString();
        }
    }
}
